using System;
using System.Web;
using System.Web.UI.WebControls;
using Eneta.Portal.Forum.Controls.Base;
using Eneta.Portal.Forum.Controls.Common;
using Eneta.Portal.Forum.Core.Domain.Entities;
using Eneta.Portal.Forum.Core.Service.Application;

namespace Eneta.Portal.Forum.Controls
{
    public class EditCategory : AdminBaseControl
    {
        private int categoryId;
        private TextBox txtName;
        private SPButton btnSubmit;
        private SPButton btnCancel;
        private Category category;

        public EditCategory()
        {
            ParentLink = ForumApplication.Instance.GetLink(SharePointForumControls.ManageForums);
        }

        protected override void CreateAdminChildControls()
        {
            CreateControls();
            LoadControlValues();
            BuildUI();
        }

        private void BuildUI()
        {
            AddBoxHeader(String.Format("Editing Category \"{0}\"", category.Name), false, 2);
            Controls.Add(CreateRow(txtName, "Category Name"));
            Controls.Add(CreateButtonRow(btnSubmit, btnCancel));
            CloseBox();
        }

        private void LoadControlValues()
        {
            categoryId = ValidInt(HttpContext.Current.Request.QueryString["category"]);
            if (categoryId == 0)
                category = new Category("New Category");
            else
                category = RepositoryRegistry.CategoryRepository.GetById(categoryId);
            txtName.Text = category.Name;
        }

        private void CreateControls()
        {
            txtName = new TextBox();

            btnSubmit = new SPButton("Submit");
            btnSubmit.Click += new EventHandler(btnSubmit_Click);

            btnCancel = new SPButton("Cancel");
            btnCancel.Click += new EventHandler(btnCancel_Click);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            RedirectToParent();
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            SaveControlValues();
            RedirectToParent();
        }

        private void SaveControlValues()
        {
            categoryId = ValidInt(HttpContext.Current.Request.QueryString["category"]);
            if (categoryId == 0)
                category = new Category("New Category");
            else
                category = RepositoryRegistry.CategoryRepository.GetById(categoryId);
            category.Name = txtName.Text;
            RepositoryRegistry.CategoryRepository.Save(category);
        }
    }
}